package util.ranker;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.lucene.queryParser.ParseException;

import util.evaluation.QueryTag;
import util.io.FileInput;
import util.io.FileOutput;
import util.ir.LMSearcherMix;

public class LocalLauncherMixStatic {

	/**
	 * @param args
	 */

//	String bigram_index = "/home/sergio/delicious_index/bigrams_index_mix";
//	String trigram_index = "/home/sergio/delicious_index/trigrams_index_mix";

	String bigram_raw = "/home/sergio/projects/delicious_index/bigrams_mix/bigrams_enriched.txt";

	// a american air www.aa.com/aa/reservation/roundTripSearchAccess.do
	// years_10_12 kids

	int _eval_query = 0;
	int _eval_suggestion = 1;
	int _eval_url = 2;
	int _eval_age = 3;
	int _eval_bucket = 4;

	//
	int _q = 0;
	int _tag = 1;
	int _freq = 2;
	int _freq_collection = 4;
	int _rank = 4;

	// 1 performances 1 1595 107

	String out_path = "/home/sergio/data/experiments/ranked_MixModel_kids/ranking_kids.txt";
	FileOutput output = new FileOutput(out_path);

	String evaluation_path = "/home/sergio/data/results/queryTestSet_clicks_kids_sample2/queryTestSet_clicks_kids_sample2.txt";
	String in_path = "/home/sergio/data/results/query_and_tags_sorted_clicks/query_and_tags_sorted_clicks.txt";

	Hashtable<String, ArrayList<QueryTag>> queries = new Hashtable<String, ArrayList<QueryTag>>();
	HashSet<String> queries_to_eval = new HashSet<String>();

	HashSet<String> vocabulary = new HashSet<String>();

	String target = "kids";
	String target_b = "kids-teens";
	LMSearcherMix trigram_searcher;
	LMSearcherMix bigram_searcher;

	private int query_threshold = 1000;
	private int query_freq_threshold = 2;

	BufferedWriter out = null;

	TagRankingMixInMemory tagRanker = null;

	public LocalLauncherMixStatic() {

		try {
			tagRanker = new TagRankingMixInMemory(target, "adults", target_b);
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (ParseException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		try {
			out = new BufferedWriter(new FileWriter(out_path));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			initChildrenQueries();
			initQueries();
			// initVocabularyMix();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void initVocabularyMix() {

		FileInput in = new FileInput(bigram_raw);

		String line = in.readString();

		while (line != null) {

			String t[] = line.split("\t");

			String a = t[1];
			String b = t[2];

			if (t[0].equals("kids-teens-mteens")) {
				vocabulary.add(a);
				vocabulary.add(b);
				// System.out.println("adding" + "\t" + a + "\t"+ b);
			}

			line = in.readString();

		}

	}

	public void initChildrenQueries() {

		FileInput in = new FileInput(evaluation_path);

		String line = in.readString();

		while (line != null) {

			String t[] = line.split("\t");
			String query = t[_eval_query];
			String bucket = t[_eval_bucket];
			if (bucket.equals("kids")) {
				queries_to_eval.add(query);

			}

			line = in.readString();

		}

	}

	public void initQueries() throws IOException {

		// trigram_searcher = new LMSearcherMix(trigram_index);
	//	bigram_searcher = new LMSearcherMix(bigram_index);

		FileInput in = new FileInput(in_path);
		String line = in.readString();
		while (line != null) {
			String t[] = line.split("\t");

			QueryTag qt = new QueryTag();

			qt.tag = t[_tag];
			qt.freq = Integer.valueOf(t[_rank]);
			// qt.collection_freq = Integer.valueOf(t[_freq_collection]);
			// qt.rank = Integer.valueOf(t[_rank]);

			String query = t[_q];

			// only add tag information of queries that are going to be
			// evaluated
			if (!queries_to_eval.contains(query)) {
				line = in.readString();
				continue;
			}

			if (queries.containsKey(query)) {

				queries.get(query).add(qt);
			} else {

				ArrayList<QueryTag> temp = new ArrayList<QueryTag>();
				temp.add(qt);
				queries.put(query, temp);
			}

			line = in.readString();

		}

	}

	

	private Iterator<Entry<String, Float>> rankQuery(String query)
			 {
		// TODO Auto-generated method stub

		Hashtable<String, Float> q = new Hashtable<String, Float>();

		q = buildQuery(query);

		Iterator<Entry<String, Float>> ranked = tagRanker.rankSuggestions(q);

		// what is below is temporal
		// Enumeration<String> keys = q.keys();
		// Hashtable<String, Float> qq = new Hashtable<String, Float>();
		// while (keys.hasMoreElements()) {//

		// String tag = keys.nextElement();
		// if (vocabulary.contains(tag)) {

		// qq.put(tag, q.get(tag));
		// }

		// }
		// Iterator<Entry<String, Float>> ranked =
		// util.hashing.Sorting.sortHashNumericValues(qq, true);

		// Iterator<Entry<String, Float>> ranked= null;
		return ranked;
	}

	private Hashtable<String, Float> buildQuery(String query) {
		// TODO Auto-generated method stub

		Hashtable<String, Float> q = new Hashtable<String, Float>();

		ArrayList<QueryTag> list = queries.get(query);

		for (int i = 0; i < list.size() && i <= query_threshold; i++) {

			q.put(list.get(i).tag, (float) list.get(i).freq);

		}

		return q;

	}

	// Output query suggestion rank

	/*public static void main(String[] args) {
		// TODO Auto-generated method stub

		LocalLauncherMixStatic launcher = new LocalLauncherMixStatic();

		launcher.rankQueries();
	}*/

}
